<?php
/**
 * 程序主入口
 */
class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        //如果存在cookie中的数据，则免去登陆的步骤
        if ($this->session->userdata('r_user_in')) {
            if ($this->session->userdata('r_role_id') == 1) {
                redirect('admin_home/index');
            } elseif ($this->session->userdata('r_role_id') == 2) {
                redirect('teacher_home/index');
            } elseif ($this->session->userdata('r_role_id') == 3) {
                redirect('student_home/index');
            } elseif ($this->session->userdata('r_role_id') == 4) {
                redirect('school_admin_home/index');
            }
        }
            
        //准备数据
        $data = array();
        $data['title'] = '电工学互联网在线考试系统';
        $data['login_state'] = "tring";
        $data['message'] = "";
        $this->load->view('home/index', $data);
    }



    public function login()
    {
        $username = $this->input->post('username', true);
        $password = $this->input->post('password', true);

        $this->load->model('user_model');
        $user = $this->user_model->check_user_valid($username, $password);
        //根据不同的角色导入到不同的页面
        if ($user) {
            if ($this->user_model->get_role($user) == 'school_admin') {
                $this->load->model('school_admin_model');
                $school_admin = $this->school_admin_model->get_by_user_id($user['id']);
                if ($school_admin) {
                    $r_user_info = array(
                        'r_username' => $user['username'],
                        'r_user_in' => true,
                        'r_role_id' => $user['role_id'],
                        'r_school_id' => $school_admin['school_id']
                    );
                }
            } elseif ($this->user_model->get_role($user) == 'teacher') {
                $this->load->model('teacher_model');
                $teacher = $this->teacher_model->get_by_user_id($user['id']);
                $teacher = $this->teacher_model->get_teacher_by_id($teacher['id']);
                if ($teacher) {
                    $r_user_info = array(
                        'r_username' => $user['username'],
                        'r_user_in' => true,
                        'r_role_id' => $user['role_id'],
                        'r_teacher_id' => $teacher['id'],
                        'r_name' => $teacher['name'],
                        'r_school_id' => $teacher['school_id'],
                        'r_school_name' => $teacher['school_name']
                    );
                }
            } elseif ($this->user_model->get_role($user) == 'student') {
                $this->load->model('student_model');
                $student = $this->student_model->get_by_user_id($user['id']);
                if ($student) {
                    $r_user_info = array(
                        'r_username' => $user['username'],
                        'r_user_in' => true,
                        'r_role_id' => $user['role_id'],
                        'r_name' => $student['name'],
                        'r_school_id' => $student['school_id']
                    );
                }
            } else {
                $r_user_info = array(
                    'r_username' => $user['username'],
                    'r_user_in' => true,
                    'r_role_id' => $user['role_id']
                );
            }
            $this->session->set_userdata($r_user_info);

            if ($this->user_model->get_role($user) == 'admin') {
                redirect('admin_home/index');
            } elseif ($this->user_model->get_role($user) == 'teacher') {
                redirect('teacher_home/index');
            } elseif ($this->user_model->get_role($user) == 'student') {
                redirect('student_home/index');
            } elseif ($this->user_model->get_role($user) == 'school_admin') {
                redirect('school_admin_home/index');
            } else {
                $this->load->view('error');
            }
        } else {
            $data['title'] = '数字资源系统';
            $data['login_state'] = "fail";
            $data['message'] = "账号或密码错误，请重试";
            $this->load->view('home/index', $data);
        }
    }
}
